﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.DataObject;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess.BusinessObject
{
    public class BCategory:Common.CDBase
    {
        /// <summary>
        /// Add new Category
        /// </summary>
        /// <param name="objCategory"></param>
        /// <returns></returns>
        public bool Add(OCategory objCategory)
        { 
            SqlParameter[] sqlParam={new SqlParameter("@CatName",SqlDbType.NVarChar),
                                        new SqlParameter("@CatSlug",SqlDbType.NVarChar),
                                        new SqlParameter("@CatParent",SqlDbType.Int),
                                        new SqlParameter("@Description",SqlDbType.NVarChar),
                                        new SqlParameter("@FeaturedImage",SqlDbType.NVarChar),
                                        new SqlParameter("@LangID",SqlDbType.VarChar),
                                        new SqlParameter("@CatType",SqlDbType.VarChar)};
            sqlParam[0].Value = objCategory.CatName;
            sqlParam[1].Value = objCategory.CatSlug;
            sqlParam[2].Value = objCategory.CatParent;
            sqlParam[3].Value = objCategory.Description;
            sqlParam[4].Value = objCategory.FeaturedImage;
            sqlParam[5].Value = objCategory.LangID;
            sqlParam[6].Value = objCategory.CatType;
            return RunProcedure("sp_tblCategory_add",sqlParam);
        }
        /// <summary>
        /// Update category
        /// </summary>
        /// <param name="objCategory"></param>
        /// <returns></returns>
        public bool Update(OCategory objCategory)
        {
            SqlParameter[] sqlParam ={ new SqlParameter("@CatID",SqlDbType.Int),
                                        new SqlParameter("@CatName",SqlDbType.NVarChar),
                                        new SqlParameter("@CatSlug",SqlDbType.NVarChar),
                                        new SqlParameter("@CatParent",SqlDbType.Int),
                                        new SqlParameter("@Description",SqlDbType.NVarChar),
                                        new SqlParameter("@FeaturedImage",SqlDbType.NVarChar),
                                        new SqlParameter("@LangID",SqlDbType.VarChar)};
            sqlParam[0].Value = objCategory.CatID;
            sqlParam[1].Value = objCategory.CatName;
            sqlParam[2].Value = objCategory.CatSlug;
            sqlParam[3].Value = objCategory.CatParent;
            sqlParam[4].Value = objCategory.Description;
            sqlParam[5].Value = objCategory.FeaturedImage;
            sqlParam[6].Value = objCategory.LangID;
            return RunProcedure("sp_tblCategory_update", sqlParam);
        }
        /// <summary>
        /// Delete Category
        /// </summary>
        /// <param name="CatID"></param>
        /// <returns></returns>
        public bool Delete(int CatID)
        {
            BCategory_Post _BCategory_Post = new BCategory_Post();
            if (_BCategory_Post.CountPost(CatID) > 0) { return false; }
            BCategory _BCategory = new BCategory();            
            IList<OCategory> lstCatChild = _BCategory.Get(0, CatID, "");
            foreach (OCategory objCategory in lstCatChild)
            {
                if (_BCategory_Post.CountPost(objCategory.CatID) > 0) { return false; }
            }            
            SqlParameter[] sqlParam = { new SqlParameter("@CatID",SqlDbType.Int)};
            sqlParam[0].Value = CatID;
            return RunProcedure("sp_tblCategory_delete", sqlParam);
        }
        /// <summary>
        /// get category by CatID, if CatID =0 get all category
        /// </summary>
        /// <param name="CatID"></param>
        /// <returns></returns>
        public IList<OCategory> Get(int CatID,string LangID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CatID",SqlDbType.Int),
                                        new SqlParameter("@LangID",SqlDbType.VarChar)};
            sqlParam[0].Value = CatID;
            sqlParam[1].Value = LangID;
            return Common.Common.ConvertTo<OCategory>(RunProcedureGet("sp_tblCategory_get", sqlParam));
        }
        /// <summary>
        /// get category by CatID and CatParent
        /// </summary>
        /// <param name="CatID"></param>
        /// <param name="Parent"></param>
        /// <returns></returns>
        public IList<OCategory> Get(int CatID, int CatParent, string LangID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CatID", SqlDbType.Int),
                                        new SqlParameter("@CatParent",SqlDbType.Int),
                                        new SqlParameter("@LangID",SqlDbType.VarChar)};
            sqlParam[0].Value = CatID;
            sqlParam[1].Value = CatParent;
            sqlParam[2].Value = LangID;            
            return Common.Common.ConvertTo<OCategory>(RunProcedureGet("sp_tblCategory_get", sqlParam));
        }
        public IList<OCategory> Get(int CatID, int CatParent, string LangID, string CatType)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CatID", SqlDbType.Int),
                                        new SqlParameter("@CatParent",SqlDbType.Int),
                                        new SqlParameter("@LangID",SqlDbType.VarChar),
                                        new SqlParameter("@CatType",SqlDbType.VarChar)};
            sqlParam[0].Value = CatID;
            sqlParam[1].Value = CatParent;
            sqlParam[2].Value = LangID;
            sqlParam[3].Value = CatType;
            return Common.Common.ConvertTo<OCategory>(RunProcedureGet("sp_tblCategory_get", sqlParam));
        }
        public IList<OCategory> Get(int CatParent, string LangID, string OrderBy, string Order, int PageIndex, int PageSize,string CatType)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CatParent",SqlDbType.Int),
                                        new SqlParameter("@LangID",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int),
                                        new SqlParameter("@CatType",SqlDbType.VarChar)};            
            sqlParam[0].Value = CatParent;
            sqlParam[1].Value = LangID;
            sqlParam[2].Value = OrderBy;
            sqlParam[3].Value = Order;
            sqlParam[4].Value = PageIndex;
            sqlParam[5].Value = PageSize;
            sqlParam[6].Value = CatType;
            return Common.Common.ConvertTo<OCategory>(RunProcedureGet("sp_tblCategory_get", sqlParam));
        }
        /// <summary>
        /// get categories by list cat id, example: listcatid="1,2,3,4"
        /// </summary>
        /// <param name="ListCatID"></param>
        /// <returns></returns>
        public IList<OCategory> Get(string ListCatID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@ListCatID",SqlDbType.VarChar) };
            sqlParam[0].Value = ListCatID;
            return Common.Common.ConvertTo<OCategory>(RunProcedureGet("sp_tblCategory_get", sqlParam));
        }
    }
}
